<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'排课列表'),
	array('op'=>'edit','title'=>'添加排课'),	
);
if($_RQ['op']=='list'){
	$params=array('area_id'=>$this->area_id);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_zhongyi_lesson_pai',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['lesson']=pdo_get('jy_zhongyi_lesson',array('id'=>$item['lessonid'],'uniacid'=>$_SESSION['uniacid']));
			$item['ontime']=$this->model('lesson')->get_uptime($item['ontime']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'tip'=>'报名科室','list'=>array(
			array('name'=>'lesson.title','title'=>'科室名称','type'=>'string'),
			array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'storeid','title'=>'所属校区','type'=>'select','options'=>'stores'),
			array('name'=>'lesson.groupid','title'=>'科室类型','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','title'=>'讲师','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'ontime','title'=>'上课时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
		)),
		'search'=>array(
			array('name'=>'area_id','width'=>10,'title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'storeid','width'=>10,'title'=>'所属校区','type'=>'select','options'=>'stores'),
			array('name'=>'groupid','width'=>10,'title'=>'科室类型','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','width'=>10,'title'=>'讲师','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'title','width'=>10,'title'=>'科室名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['stores']=pdo_getall('jy_zhongyi_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['areas']=pdo_getall('jy_zhongyi_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_zhongyi_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=pdo_getall('jy_zhongyi_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_zhongyi_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['ontime']=date('Y-m-d H:i:s',$item['ontime']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回科室列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'lessonid','title'=>'科室选择','type'=>'choose','action'=>'get_lesson'),
					array('name'=>'storeid','title'=>'所属校区','type'=>'select_more','options'=>'stores'),
					//array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
					array('name'=>'storetype','title'=>'分校类型','type'=>'radio','options'=>'storetype'),
					array('name'=>'workerid','title'=>'讲师选择','type'=>'choose','action'=>'get_worker'),
					array('name'=>'ontime','title'=>'上课时间','type'=>'time'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['storetype']=$this->storetype;
	$return['htmls']=$htmls;
	$return['areas']=pdo_getall('jy_zhongyi_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['stores']=pdo_getall('jy_zhongyi_store',array('area_id'=>$this->area_id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_zhongyi_lesson_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['lessonid']){
		exi('请选择科室！','error');
	}
	if(!$postdata['storeid']){
		exi('请选择校区！','error');
	}
	if(!$postdata['workerid']){
		exi('请选择讲师！','error');
	}
	$data=array_fields(array('ontime','lessonid','workerid','storeid','storetype','person_limit','paixu','status'),$postdata);
	$id=intval($postdata['id']);
	$data['ontime']=strtotime($data['ontime']);
	$data['weekid']=date("w",$data['ontime']);
	if($id){
		pdo_update('jy_zhongyi_lesson_pai',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['area_id']=$this->area_id;
		pdo_insert('jy_zhongyi_lesson_pai',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	$lesson=pdo_getall('jy_zhongyi_lesson_pai',$params);
	foreach($lesson as &$v){
		$v['starttime']=date('Y-m-d',$v['starttime']);
		$v['endtime']=date('Y-m-d',$v['endtime']);
		$v['storetitle']=pdo_getcolumn('jy_zhongyi_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$v['areatitle']=pdo_getcolumn('jy_zhongyi_area',array('id'=>$v['area_id']),'title');
		$v['grouptitle']=pdo_getcolumn('jy_zhongyi_lesson_group',array('id'=>$v['groupid']),'title');
		$v['workertitle']=pdo_getcolumn('jy_zhongyi_worker',array('id'=>$v['workerid']),'name');
	}
	$header=array(
		'id'=>'科室ID',
		'title'=>'科室名称',
		'areatitle'=>'所属区域',
		'storetitle'=>'所属校区',
		'grouptitle'=>'科室类型',
		'workertitle'=>'讲师',
		'starttime'=>'开始时间',
		'endtime'=>'结束时间',
	);
	cfc('tools')->export2excel($header,$lesson,'科室表');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_zhongyi_lesson_pai',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的科室！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_zhongyi_lesson_pai',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}